$(function () {

//    $("#search_lskh_qc").qyjgzcombobox();
//    $("#search_lskh_jc").qyjgzcombobox();
    //页面初始加载
    $('#lskh_grid').datagrid({
        url: $.webPath() + "/organ/qyjg/lskhList",
        toolbar: "#lskh_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: "true",
        columns: [
            [
                {field: 'qc', title: '全称', width: 50},
                {field: 'jc', title: '简称', width: 40},
                {field: 'qyfrh', title: '营业执照注册号', width: 40},
                {field: 'zzjgh', title: '组织机构号', width: 40},
                {field: 'nsh', title: '纳税号', width: 40},
                {field: 'bm', title: '编码', width: 30},
                {field: 'bz', title: '备注', width: 60},
                {field: 'sfzx', title: '启用', align: 'center', width: 10, formatter: function (value, row, index) {
                    if (value == '是')
                        return '否';
                    else
                        return '是';
                }}
            ]
        ],
        onLoadSuccess: function () {
            $("#lskh_mod").hide();
            $("#lskh_del").hide();
            $("#lskh_start").hide();
            $("#lskh_gl").hide();
            $("#lskh_qxgl").hide();
        },
        onClickRow: function (rowIndex, rowData) {
            $("#lskh_mod").show();
            $("#lskh_del").show();
            $("#lskh_gl").show();
            $("#lskh_qxgl").show();
            if (rowData.sfzx == null || rowData.sfzx == '是') {
                $("#lskh_start").text('启用');
                $("#lskh_del").show();
                $("#lskh_start").removeClass('stop').addClass('start');
                $('#lskh_start').unbind('click');
                $('#lskh_start').bind('click', function () {
                    changelskhState($.webPath() + '/organ/lskh/start_qyjg', '确认启用此机构？');
                });
            } else {
                $("#lskh_start").text('停用');
                $("#lskh_del").hide();
                $("#lskh_start").removeClass('start').addClass('stop');
                $('#lskh_start').unbind('click');
                $('#lskh_start').bind('click', function () {
                    changelskhState($.webPath() + '/organ/lskh/stop_qyjg', '确认停用此机构？');
                });
            }
            $("#lskh_start").show();
        },
        onDblClickCell: function (rowIndex, field, value) {
            if (field == "qc")
                $("#lskh_mod:visible").click();
        },
        rowStyler: function (index, row) {
            if (row.sfzx == '是') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    });

    //**************按钮绑定事件*************
    //搜索按钮触发
    $('#lskh_search').click(
        lskh_search
    );

    //零售客户导出
    $("#lskh_dc").click(function () {
        window.open($.webPath() + "/organ/lskh/qyjg_export");
    });

    //页面新增按钮触发
    $('#lskh_add').click(
        lskh_newItem
    );
    //删除按钮触发
    $('#lskh_del').click(
        lskh_deleteItem
    );
    //修改按钮触发
    $('#lskh_mod').click(
        lskh_modifyItem
    );
    //启用按钮触发
    $("#lskh_start").click(function () {
        changelskhState();
    });

    //**************按钮绑定事件*************
});

var lskhWin;
//新建弹出框
function lskh_newItem() {

    var title = '新增零售客户';
    var url = $.webPath() + '/organ/lskh/lskh_content';

    lskhWin = $.window({
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        },
        title: title,
        width: 600,
        height: 420,
        cache: false,
        iconCls: 'icon-add2',
        url: url,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    var selector = "#lskhForm";
                    var valid = $(selector).form('validate');
                    if (valid) {
                        lskh_saveItem();
                    }
                }
            }
        ]
    });
}
//修改弹出框
function lskh_modifyItem() {
    var lskhId = $("#lskh_grid").datagrid("getSelected").id;

    if (lskhId) {
        var title = '修改零售客户';
        var url = $.webPath() + '/organ/lskh/lskh_content?qyjgId=' + lskhId;

        lskhWin = $.window({
            onBeforeClose: function () {
                $(".validatebox-tip").remove();
            },
            title: title,
            width: 590,
            height: 450,
            closed: false,
            cache: false,
            iconCls: 'icon-edit',
            url: url,
            modal: true,
            buttons: [
                {
                    text: '保存',
                    handler: function () {
                        var selector = "#lskhForm";
                        var valid = $(selector).form('validate');
                        if (valid) {
                            lskh_saveUpdateItem();
                        }
                    }
                }
            ]
        });
    }
}
//新增保存
function lskh_saveItem() {
    var selector;
    selector = "#lskhForm";
    var url = $.webPath() + '/organ/qyjg/add_lskh';
    $(selector).form('submit', {
        url: url,
        onSubmit: function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');
            }
            return isValid;
        },
        success: function (data) {
            var obj = eval('(' + data + ')');
            if (obj.statusCode == 200) {
                $(lskhWin).window("close");
                $('#lskh_grid').datagrid('reload');
            } else {
                alert(obj.message);
            }
        }
    });
}
//保存弹出框
function lskh_saveUpdateItem() {
    var lskhId = $("#lskh_grid").datagrid("getSelected").id;
    var selector = "#lskhForm";
    var url = $.webPath() + '/organ/lskh/update_lskh?qyjgId=' + lskhId;
    $(selector).form('submit', {
        url: url,
        onSubmit: function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');
            }
            return isValid;
        },
        success: function (data) {
            var obj = eval('(' + data + ')');
            if (obj.statusCode == 200) {
                $(lskhWin).window("close");
                $('#lskh_grid').datagrid('reload');
            } else {
                alert(obj.message);
            }
        }
    });
}
//删除企业机构
function lskh_deleteItem() {
    var lskhId = $("#lskh_grid").datagrid("getSelected").id;
    if (lskhId) {
        var url = $.webPath() + "/organ/qyjg/delete_qyjg?qyjgId=" + lskhId;
        var message = "是否确认删除此零售客户";
        $.messager.confirm('确认', message, function (r) {
            if (r) {
                $.ajax({
                    url: url,
                    type: "json",
                    success: function (data) {
                        if (data.statusCode == 200) {
                            $.messager.show({
                                title: '提示框',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $('#lskh_grid').datagrid('reload');
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        });
    }
}
//查询企业机构
function lskh_search() {
    var nText1 = $('#search_lskh_qc').val() == null ? null : $('#search_lskh_qc').val();
    var nText = $('#search_lskh_jc').val() == null ? null : $('#search_lskh_jc').val();

    var options = $.extend(getlskhGridOption(),
        {queryParams: {qc: nText1,jc:nText}});
    $('#lskh_grid').datagrid(options);
}
//页面切换的内容
function getlskhGridOption() {
    var options = {
        url: $.webPath() + "/organ/qyjg/lskhList",
        toolbar: "#lskh_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber:1,
        fit: true,
        rownumbers: "true",
        onLoadSuccess: function () {
            $("#lskh_mod").hide();
            $("#lskh_del").hide();
            $("#lskh_start").hide();
            $("#lskh_gl").hide();
            $("#lskh_qxgl").hide();
        },
        onClickRow: function (rowIndex, rowData) {
            $("#lskh_mod").show();
            $("#lskh_del").show();
            $("#lskh_gl").show();
            $("#lskh_qxgl").show();
            if (rowData.sfzx == null || rowData.sfzx == '是') {
                $("#lskh_start").text('启用');
                $("#lskh_del").show();
                $("#lskh_start").removeClass('stop').addClass('start');
                $('#lskh_start').unbind('click');
                $('#lskh_start').bind('click', function () {
                    changelskhState($.webPath() + '/organ/lskh/start_qyjg', '确认启用此机构？');
                });
            } else {
                $("#lskh_start").text('停用');
                $("#lskh_del").hide();
                $("#lskh_start").removeClass('start').addClass('stop');
                $('#lskh_start').unbind('click');
                $('#lskh_start').bind('click', function () {
                    changelskhState($.webPath() + '/organ/lskh/stop_qyjg', '确认停用此机构？');
                });
            }
            $("#lskh_start").show();
        },
        onDblClickCell: function (rowIndex, field, value) {
            if (field == "qc")
                $("#lskh_mod:visible").click();
        },
        rowStyler: function (index, row) {
            if (row.sfzx == '是') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    };

    var columns = {columns: [
        [
            {field: 'qc', title: '全称', width: 50},
            {field: 'jc', title: '简称', width: 30},
            {field: 'qyfrh', title: '营业执照注册号', width: 30},
            {field: 'zzjgh', title: '组织机构号', width: 30},
            {field: 'nsh', title: '纳税号', width: 30},
            {field: 'bm', title: '编码', width: 30},
            {field: 'bz', title: '备注', width: 60},
            {field: 'sfzx', title: '启用', align: 'center', width: 10, formatter: function (value, row, index) {
                if (value == '是')
                    return '否';
                else
                    return '是';
            }}
        ]
    ]}
    options = $.extend(options, columns);

    return options;
}
//改变状态，启用停用
function changelskhState(url, message) {
    var lskhId = $("#lskh_grid").datagrid("getSelected").id;
    if (lskhId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url: url + "?qyjgId=" + lskhId,
                    type: "json",
                    success: function (data) {
                        if (data.statusCode == 200) {
                            $('#lskh_grid').datagrid('reload');
                            $.messager.show({
                                title: '提示框',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}